//
//  Goods.m
//  EasyToo
//
//  Created by zhengzhe on 13-9-10.
//
//

#import "Goods.h"

@implementation Goods

- (id)initWithDictionary:(NSDictionary *)dict
{
    self = [self init];
    if (self) {
        self.goodsId = GetDictionaryStringDefaultNil(dict, @"id");
        self.name = GetDictionaryStringDefaultNil(dict, @"title");
        self.imgUrl = GetDictionaryStringDefaultNil(dict, @"terminal_img");
        self.desc= GetDictionaryStringDefaultNil(dict, @"note");
        self.oriPrice = [GetDictionaryNumberDefaultNil(dict, @"cost_price") floatValue];
        self.salePrice = [GetDictionaryNumberDefaultNil(dict, @"sale_price") floatValue];
        self.discount = GetDictionaryIntDefaultZero(dict, @"discount");
        self.uint = GetDictionaryStringDefaultNil(dict, @"unit");
        
        if (!self.imgUrl) {
            self.imgUrl = GetDictionaryStringDefaultNil(dict, @"web_img");
        }
        
        //推荐的
        if (!self.name) {
            self.name = GetDictionaryStringDefaultNil(dict, @"title");
        }
        
        
        NSArray *jsonArray = GetDictionaryArray(dict, @"showProperties");
        
        if (jsonArray && [jsonArray count] > 1)
        {
            NSDictionary *saleJsonDict = [jsonArray objectAtIndex:0];
            self.saleInfo = GetDictionaryStringDefaultNil(saleJsonDict, @"value");
            
            NSDictionary *freightJsonDict = [jsonArray objectAtIndex:1];
            self.freightInfo = GetDictionaryStringDefaultNil(freightJsonDict, @"value");
        }

    }
    return self;
}

- (void)dealloc
{
    self.goodsId = nil;
    self.name = nil;
    self.imgUrl = nil;
    self.oriPrice = 0;
    self.salePrice = 0;
    self.discount = 0;
    self.uint = nil;
    self.saleInfo = nil;
    self.freightInfo = nil;
    [super dealloc];
}

@end
